package App.Service;

import App.Model.SMS.SMSCheckCodeRequest;
import App.Model.SMS.SMSCheckPhoneRequest;
import App.Model.SMS.SMSGetCodeRequest;
import App.Repository.SMSRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Random;

/**
 * Created by xw on 2019/1/22.
 */

@Service
public class SMSService {

    @Autowired
    private SMSRepository _repository;


    @Transactional
    public boolean sendSms(SMSGetCodeRequest _request) throws Exception {
        String _phone = _request.getPhone();
        boolean _result = false;
        if (!_phone.isEmpty()) {
            String _code = String.valueOf(new Random().nextInt(8999) + 1000);//生成短信验证码
            if (_request.getSmstype().equals("voice")) {
                _result = _repository.sendVoiceMessage(_request.getPhone(), "", _code);
            } else {
                String _formatCode =_code + "为法力登录验证码,一分钟后失效，请根据页面提示数据如验证码完成验证。请勿向任何人提供验证码，谨防诈骗";
                _result = _repository.sendMessage(_request.getPhone(), _formatCode, _code);
            }
        }
        return _result;
    }
    public boolean checkCode(SMSCheckCodeRequest _request) throws Exception {
        String _phone = _request.getPhone();
        boolean _result = false;
        if (!_phone.isEmpty()) {

            _result = _repository.checkSMScode(_request);
        }

        return _result;
    }


    public boolean checkPhone(SMSCheckPhoneRequest _request) throws Exception {
        String _phone = _request.getPhone();
        boolean _result = false;
        if (!_phone.isEmpty()) {
            _result = _repository.checkPhone(_request);
        }
        return _result;
    }


}
